﻿using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

namespace RssReader.DataModel
{
    public class Item : INotifyPropertyChanged
    {
        private string _PublishedDate = string.Empty;
        private string _Category = string.Empty;
        private string _Content = string.Empty;
        private string _Description = string.Empty;
        private ImageSource _Image;
        private string _Link = string.Empty;
        private string _Title = string.Empty;

        public string Title
        {
            get { return _Title; }

            set
            {
                if (_Title != value)
                {
                    _Title = value;
                    OnPropertyChanged("Title");
                }
            }
        }

        public string PublishedDate
        {
            get { return _PublishedDate; }

            set
            {
                if (_PublishedDate != value)
                {
                    _PublishedDate = value;
                    OnPropertyChanged("PublishedDate");
                }
            }
        }

        public ImageSource Image
        {
            get { return _Image; }

            set
            {
                if (_Image != value)
                {
                    _Image = value;
                    OnPropertyChanged("ImageUrl");
                }
            }
        }

        public string Link
        {
            get { return _Link; }

            set
            {
                if (_Link != value)
                {
                    _Link = value;
                    OnPropertyChanged("Link");
                }
            }
        }

        private Category _category;
        public Category Category
        {
            get { return _category; }

            set { this.SetProperty(ref this._category, value); }
        }

        public string Description
        {
            get { return _Description; }

            set
            {
                if (_Description != value)
                {
                    _Description = value;
                    OnPropertyChanged("Description");
                }
            }
        }

        public string Content
        {
            get { return _Content; }

            set
            {
                if (_Content != value)
                {
                    _Content = value;
                    OnPropertyChanged("Content");
                }
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public void SetImage(Uri baseUri, String path)
        {
            Image = new BitmapImage(new Uri(baseUri, path));
        }

        /// <summary>
        /// Checks if a property already matches a desired value.  Sets the property and
        /// notifies listeners only when necessary.
        /// </summary>
        /// <typeparam name="T">Type of the property.</typeparam>
        /// <param name="storage">Reference to a property with both getter and setter.</param>
        /// <param name="value">Desired value for the property.</param>
        /// <param name="propertyName">Name of the property used to notify listeners.  This
        /// value is optional and can be provided automatically when invoked from compilers that
        /// support CallerMemberName.</param>
        /// <returns>True if the value was changed, false if the existing value matched the
        /// desired value.</returns>
        protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
        {
            if (object.Equals(storage, value)) return false;

            storage = value;
            this.OnPropertyChanged(propertyName);
            return true;
        }
    }
}